
from flask import Blueprint, request, render_template
from sqlalchemy import desc
from applications.common.utils.http import  table_api, success_api, fail_api
from applications.common.utils.rights import authorize
from applications.models import AdminLog
from applications.schemas import LogOutSchema
from applications.common.curd import model_to_dicts
from applications.common.utils.transactional import transactional
from applications.common.utils.validate import str_escape
from applications.extensions import db
from applications.common import curd
from applications.models import YeOperationDetection
from applications.models import YeOperationSetting
from applications.schemas import YeOperationDetectionSchema
from applications.schemas import YeOperationSettingSchema
import json

'''
操作设置
'''
operationSettingView = Blueprint('operationSettingView', __name__, url_prefix='/ye/operationSetting')



## 分组相关接口
## 分组相关接口
# 增加修改
@operationSettingView.post('/saveOrUpdate')
@transactional(rollback = True)
def saveOrUpdate():
    # req_json = request.get_json(force=True)
    requestData = request.get_data()
    req_json = json.loads(requestData)
    for item in req_json :
        tempData =  dict(
            id = item.get("id"),
            alias = str_escape(item.get("alias")),
            remarks = str_escape(item.get("remarks")),
            operation_type = str_escape(item.get("operation_type")),
            position_way = str_escape(item.get("position_way")),
            position_x = item.get("position_x"),
            position_y = item.get("position_y"),
            wait_time = item.get("wait_time"),
            picture_url = str_escape(item.get("picture_url")),
            feature_point = item.get("feature_point"),
            operation_order = item.get("operation_order"),
            sliding_length = item.get("sliding_length"),
            slide_direction = item.get("slide_direction"),
            text_content = str_escape(item.get("text_content")),
            detection_id =  item.get("detection_id"),
        )
        if id is None : 
            # 增加
            d = YeOperationDetection(tempData)
            db.session.add(d)
        else:
            # 修改
            d = YeOperationDetection.query.filter_by(id=id)
            d.update(tempData)
    return success_api(msg="增加或修改成功")


# 查询，通过类型查全部
@operationSettingView.get('/query')
def query():
    # 获取请求参数
    detection_id = request.args.get('detection_id', type=int)
    if detection_id is None : fail_api(msg="查询参数不全")
    # orm查询
    # 使用分页获取data需要.items
    dict_all = YeOperationSetting.query.filter_by(detection_id=detection_id).extjs_paginate()
    count = dict_all.total
    data = curd.model_to_dicts(schema=YeOperationSettingSchema, data=dict_all.items)
    return table_api(data=data, count=count)



# 删除，并删除操作
@operationSettingView.get('/delete')
@transactional(rollback = True)
def delete():
    # 获取请求参数
    idsStr = request.args.get('ids', type=str)
    idList = map(lambda item : int(item), idsStr.split(','))
    # YeOperationDetection.query.filter(YeOperationDetection.id.in_(idList)).delete()
    YeOperationSetting.query.filter(YeOperationSetting.id.in_(idList)).delete()
    return success_api(msg="删除成功")